window.onload=function(){
    showTable();
};
var jsonAll;
var jsonSe;
var totalNums=0;
var currentPage=1;
var pageset=7;
var prePage = 1;

/**
 *@Author llh
 *@Description TODO 分页
 *@Date 14:32 2019/4/10
 *@Modify by
 *@Version 1.0
 */
function pageSplite(currentPage){
    totalNums = jsonAll.length;
    document.getElementById("totalCount").innerText=totalNums;
    var totalPage = Math.ceil(totalNums/pageset);//ceil 余数加
    for (var i = 1; i <= prePage; i++) {    //清除之前的页码
        $("#" + i + "").remove();
    }

    if(jsonAll.length== 0){
        document.getElementById("tbody").innerHTML =  "<tr><td valign=\"top\" colspan=\"14\" >没有数据</td></tr>";
    }else{
        show(jsonAll,currentPage,totalPage);
    }

}
function show(jsonAll, currentPage, totalPage){
    var tb=document.getElementById("tb");
    var length = tb.tBodies[0].rows.length;
    for(i=0;i<length;++i){
        tb.tBodies[0].deleteRow(0);//每次删除第一行
    }
    var begin=(currentPage-1)*pageset;
    var end=currentPage*pageset;
    end=end>jsonAll.length?jsonAll.length:end;
    var getOrConsigee=null;
    var stat=null;
    for(var i = begin; i < end; i++){
        var row = tb.tBodies[0].insertRow();
        row.insertCell().innerHTML = "<input type='checkbox'/>";
        row.insertCell().innerText = i+1;
        row.insertCell().innerText = jsonAll[i].orderId;
        row.insertCell().innerText = jsonAll[i].accountNo;
        row.insertCell().innerText = jsonAll[i].orderTime;
        getOrConsigee = jsonAll[i].getOrConsigee;
        if (getOrConsigee == "1") {
            row.insertCell().innerText = "取货";
        } else {
            row.insertCell().innerText = "送货";
        }
        row.insertCell().innerText = jsonAll[i].consigeeAddr
        row.insertCell().innerText = jsonAll[i].buyPrice;
        row.insertCell().innerText = jsonAll[i].realPayPrice;
        stat=jsonAll[i].orderState;
        if (getOrConsigee == "1") {
            if (stat == "1") {
                row.insertCell().innerText = "未取货";
            } else if (stat == "2") {
                row.insertCell().innerText = "已取货";
            } else if (stat == "0") {
                row.insertCell().innerText = "未付款";
            }
        } else {
            if (stat == "1") {
                row.insertCell().innerText = "未发货";
            } else if (stat == "2") {
                row.insertCell().innerText = "已发货";
            } else if (stat == "3") {
                row.insertCell().innerText = "已收货";
            } else if (stat == "0") {
                row.insertCell().innerText = "未付款";
            }
        }
        row.insertCell().innerHTML = "<a style='text-decoration:none' class='ml-5' onClick='order_Detail(" + jsonAll[i].orderId + "," + jsonAll[i].accountNo + ")'>" +
            " <i class='Hui-iconfont'>&#xe6df;</i></a>&nbsp;&nbsp;<a style='text-decoration:none' class='ml-5' onClick='product_del(" + jsonAll[i].orderId + "," + jsonAll[i].accountNo + ")'" +
            "title='删除'>" +
            "<i class='Hui-iconfont'>&#xe6e2;</i></a>";
    }
    changeAlign();
    pageview(currentPage,totalPage);
    for (var i = 1; i <= totalPage; i++) {
        if (currentPage == i) {
            document.getElementById(currentPage).setAttribute("class", "active");
        } else {
            document.getElementById(i).removeAttribute("class", "active");
        }
    }
}
function pageview(currentPage,totalPage) {
    var pages = document.getElementById("pages");
    for (var i = 1; i <= totalPage; i++) {
        var elem_li = document.createElement('li'); // 生成一个 li元素
        elem_li.setAttribute("id", i);
        elem_li.innerHTML = "<a href = '#' >" + i + "</a>"; // 设置元素的内容
        elem_li.onclick = function () {
            currentPage = $(this).find("a").eq(0).text();
            pageSplite(currentPage);
        }
        pages.insertBefore(elem_li, document.getElementById("next")); // 添加到UL中去
    }
    prePage = totalPage;
    /*上一页和下一页*/
    var pre = document.getElementById("previous");
    var nex = document.getElementById("next");
    pre.onmouseover = function () {
        if (currentPage <= 1) {
            this.setAttribute("class", "disabled");
        } else {
            this.removeAttribute("class", "disabled");
        }
    }
    nex.onmouseover = function () {
        if (currentPage >= totalPage) {
            this.setAttribute("class", "disabled");
        } else {
            this.removeAttribute("class", "disabled");
        }
    }
    pre.onclick = function () {
        if (currentPage <= 1) {
            currentPage = 1;
        } else {
            currentPage = currentPage - 1;
        }
        pageSplite(currentPage);
    }
    nex.onclick = function () {
        if (currentPage >= totalPage) {
            currentPage = totalPage;
        } else {
            currentPage = currentPage + 1;
        }
        pageSplite(currentPage);
    }
}
function changeAlign(){
    var t = document.getElementById("tbody");
    var x =t.getElementsByTagName("td");
    for (var i = 0; i<x.length; i++) {

        x[i].style = "text-align : center; vertical-align : middle";
    }
}

function order_Detail(orderId,accountNo){
    localStorage.setItem('orderId',orderId);
    localStorage.setItem('accountNo',accountNo);
    window.location.href="purOrderDetailManager.html";

}
/**
 *@Author llh
 *@Description TODO 订单显示
 *@Date 14:32 2019/4/10
 *@Modify by
 *@Version 1.0
 */
function showTable(){
    var date=null;
    var url="purchase!getAllPurchaseOrderList";
    sendRequest("post", url, date, ShowAllBack);
}

function ShowAllBack(result){
    if(result == '"nopurchaseorder"'){
        var tb=document.getElementById("tb");
        var length = tb.tBodies[0].rows.length;
        for(var i=0;i<length;++i){
            tb.tBodies[0].deleteRow(0);//每次删除第一行
        }
        document.getElementById("tbody").innerHTML =  "<tr><td valign=\"top\" colspan=\"14\" >没有数据</td></tr>";
        jsonSe=jsonAll=null;
    }else{
        var json=JSON.parse(result);
        jsonSe = new Function("return" + json)();
        jsonAll=jsonSe;
        pageSplite(currentPage);

    }
}
/**
 *@Author llh
 *@Description TODO 订单状态搜索
 *@Date 14:14 2019/4/10
 *@Modify by
 *@Version 1.0
 */
function SearchState(){
    var getstate = document.getElementById("state");
    var state = getstate.options[getstate.selectedIndex].value;
    var jsonState = [];
    if(state!=4){
        for(var i=0;i<jsonSe.length;i++){
            if(jsonSe[i].orderState==state){
                jsonState.push(jsonSe[i]);
            }
        }
    }else{
        jsonState=jsonSe;
    }
    currentPage=1;
    jsonAll=jsonState;
    pageSplite(currentPage);
}
/**
 *@Author llh
 *@Description TODO 订单搜索
 *@Date 14:13 2019/4/10
 *@Modify by
 *@Version 1.0
 */
function SearchOrder() {
    var accountNo = document.getElementById("accountNo").value;
    var timein = document.getElementById("timein").value;
    var timeout = document.getElementById("timeout").value;
    var dat = new Date();
    if(timeout!=""&&timein==""){
        alert("请输入查询日期开始时间");
        return;
    }
    if(timein!=""&&timeout==""){ timeout = dat.getTime(); }
    var date = "&accountNo="+accountNo+"&timein=" + timein + "&timeout="+timeout;
    // console.log(date);
    var url = "purchase!optionSelect";
    sendRequest("post", url, date, ShowAllBack);
}